package itsm.isperp.report.resolver;

import itsm.isperp.framework.utils.JsonBuilder;
import itsm.isperp.module.entity.kpi.KpiSetting;
import itsm.isperp.report.IDataResolver;
import itsm.isperp.report.data.warehouse.QueryRequest;
import itsm.isperp.report.data.warehouse.QueryResponse;
import itsm.isperp.report.paser.QueryConditionPaser;
import itsm.isperp.report.paser.QueryLineLegend;
import itsm.isperp.report.paser.QueryLinePaser;
import itsm.isperp.report.paser.QueryRequestPaser;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.data.jdbc.support.DatabaseType;

import me.chanjar.weixin.common.util.StringUtils;

import com.github.abel533.echarts.axis.AxisLine;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Gauge;
import com.github.abel533.echarts.series.Series;
import com.github.abel533.echarts.series.gauge.Detail;
import com.github.abel533.echarts.style.ItemStyle;
import com.github.abel533.echarts.style.LineStyle;

/**
 * 
 * 数据分解
 * 
 * @author lizx
 * @date 2015-12-3
 * @since 1.0
 * 
 */
public abstract class BaseResolver implements IDataResolver {

	protected KpiSetting setting;

	protected QueryRequest queryRequest;

	protected QueryResponse queryResponse;

	protected String group;

	protected String dtype;

	protected String stype;

	protected boolean useTotal = false;

	QueryLineLegend qll = null;

	StringBuilder sb = null;

	public BaseResolver(KpiSetting setting, String dtype, String group,
			String stype, boolean queryLine, HttpServletRequest req)
			throws ParseException {
		this.setting = setting;
		this.dtype = dtype;
		this.group = group;
		this.stype = stype;
		this.queryResponse = QueryConditionPaser
				.paserQueryContitionFromRequest(false, false, this.setting,
						dtype, req);

		buildRequest();

		sb = queryResponse.getWhereSql();

		qll = QueryLinePaser.paser(setting, dtype, sb,
				queryResponse.getStartDate(), queryResponse.getEndDate(),
				queryLine);
	}

	public static String[] ITEMS_STYLE_COLOR = new String[] { "#C1232B",
			"#B5C334", "#FCCE10", "#E87C25", "#27727B", "#FE8463", "#9BCA63",
			"#FAD860", "#F3A43B", "#60C0DD", "#D7504B", "#C6E579", "#F4E001",
			"#F0805A", "#26C0C0" };

	protected static ItemStyle getItsmstyle(int i) {
		ItemStyle is = new ItemStyle();
		if (i < ITEMS_STYLE_COLOR.length)
			is.normal().color(ITEMS_STYLE_COLOR[i]);
		else {
			is.normal().color("#FFFACD");
		}
		return is;

	}

	public String getStype() {
		return stype;
	}

	public void setStype(String stype) {
		this.stype = stype;
	}

	public boolean isUseTotal() {
		return useTotal;
	}

	public void setUseTotal(boolean useTotal) {
		this.useTotal = useTotal;
	}

	public KpiSetting getSetting() {
		return setting;
	}

	public void setSetting(KpiSetting setting) {
		this.setting = setting;
	}

	public QueryResponse getQueryResponse() {
		return queryResponse;
	}

	public void setQueryResponse(QueryResponse queryResponse) {
		this.queryResponse = queryResponse;
	}

	public QueryRequest getQueryRequest() {
		return queryRequest;
	}

	protected abstract GsonOption resolveValue();

	protected abstract GsonOption resolveRatio();

	protected abstract GsonOption resolveValueLine();

	protected abstract GsonOption resolveRatioLine();

	public GsonOption resolve() {
		GsonOption o = null;
		// 判断 pie 还是 ratio，通过不同的代理器检索数据
		if ("饼图".equals(this.getSetting().getShowType())) {
			o = resolveValue();
		} else {
			o = resolveRatio();
			Series<?> se = o.getSeries().get(0);

			if (se instanceof Gauge) {
				Gauge gg = (Gauge) se;
				if (StringUtils.isNotEmpty(setting.getShowUnit())) {
					Detail d = new Detail();
					d.setFormatter("{value}" + setting.getShowUnit());
					gg.setDetail(d);
				}

				if (StringUtils.isNotEmpty(setting.getShowRateMax())) {
					gg.setMax(Integer.valueOf(setting.getShowRateMax()));
				}
				if (StringUtils.isNotEmpty(setting.getShowRateLineStyle())) {
					AxisLine axisLine = new AxisLine();

					LineStyle ls = JsonBuilder.buildNormalBinder().fromJson(
							setting.getShowRateLineStyle(), LineStyle.class);
					axisLine.setLineStyle(ls);
					gg.setAxisLine(axisLine);
				}
			}

		}

		return o;
	}

	public GsonOption resolveLine() {
		GsonOption o = null;
		// 判断 pie 还是 ratio，通过不同的代理器检索数据
		if ("饼图".equals(this.getSetting().getShowType())) {
			o = resolveValueLine();
		} else {
			o = resolveRatioLine();
		}
		return o;
	}

	/**
	 * 
	 * @return
	 */
	private void buildRequest() {
		queryRequest = new QueryRequest();

		queryRequest.setFrom(setting.getQueryFrom());
		String g = null;
		if (group == null || "null".equals(group)) {
			g = setting.getQueryGroup();
		} else {
			String moduleName=setting.getModuleName();
			g = QueryRequestPaser.parseRequest(group, queryRequest,moduleName);

		}
		queryRequest.setGroup(g);

		if (StringUtils.isNotBlank(setting.getQueryColumn())) {
			queryRequest.addColumn(setting.getQueryColumn());
		}

		this.useTotal = (this.group == null);
		if (StringUtils.isEmpty(g)) {
			queryRequest.setWhere((queryRequest.getWhere() == null ? ""
					: queryRequest.getWhere()));
		} else {

			if (itsm.isperp.framework.data.dialect.DialectFactory
					.getDatabaseType() == DatabaseType.MYSQL) {
				queryRequest.addColumn("IFNULL(" + g + ",'空') AS name");
			} else {

				queryRequest.addColumn("NVL(" + g + ",'空') AS name");
			}

			queryRequest.setWhere(queryRequest.getWhere() == null ? ""
					: queryRequest.getWhere());

			queryRequest.setGroupBy(g);

		}

	}

}
